﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EC21_EDiamond
{
    public partial class Cart : System.Web.UI.Page
    {
        private DataTable dsGioHang;
        private void LoadData()
        {
            if(Request.QueryString["MaSP"] != null)
            {
                
            }
            if (rpDsSpGioHang.Items.Count > 0)
            {
                Int64 tongtien = 0;
                for (int i = 0; i < rpDsSpGioHang.Items.Count; i++)
                {
                    Label lbtotalprice = (Label)rpDsSpGioHang.Items[i].FindControl("lbTongGia");
                    lbtotalprice.Text = lbtotalprice.Text.Replace(" VNĐ","");
                    tongtien += Int64.Parse(lbtotalprice.Text);
                    lbtotalprice.Text = lbtotalprice.Text + " VNĐ";
                }
                lbtTotal1.Text = tongtien.ToString();
                lbtTotal2.Text = tongtien.ToString();
            }
        } 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["GioHang"] == null)
                {
                    Response.Redirect("Default.aspx");
                }
                else
                {
                    dsGioHang = new DataTable();
                    dsGioHang = (DataTable)Session["GioHang"];
                    rpDsSpGioHang.DataSource = dsGioHang;
                    rpDsSpGioHang.DataBind();
                }
                LoadData();
            }
        }

        protected void btnTieptuc_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }

        private void UpdateGioHang(DataTable dt, int maSP, int sl)
        {
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (Int32.Parse(dt.Rows[i]["MaSP"].ToString()) == maSP)
                    {
                        dt.Rows[i]["SoLuong"] = sl;
                        dt.Rows[i]["TongTien"] = sl * Int32.Parse(dt.Rows[i]["Gia"].ToString());
                        break;
                    }
                }
            }
        }

        protected void btnCapNhap_Click(object sender, EventArgs e)
        {
            Label maSP;
            Label total;
            for(int i=0;i<rpDsSpGioHang.Items.Count;i++)
            {
                maSP = (Label)rpDsSpGioHang.Items[i].FindControl("lbMaSP");
                TextBox soluong = (TextBox)rpDsSpGioHang.Items[i].FindControl("txtSL");
                Label gia = (Label)rpDsSpGioHang.Items[i].FindControl("lbGia");
                total = (Label)rpDsSpGioHang.Items[i].FindControl("lbTongGia");
                if (soluong.Text.Trim().Equals("0"))
                {
                    soluong.Text = "1";
                    total.Text = (Int32.Parse(gia.Text.ToString().Replace(" VNĐ", "")) * Int32.Parse(soluong.Text.ToString()) + " VNĐ");
                }else{
                    total.Text = (Int32.Parse(gia.Text.ToString().Replace(" VNĐ", "")) * Int32.Parse(soluong.Text.ToString()) + " VNĐ");
                }
                //lbTongGia.Text = total.Text;
                UpdateGioHang(((DataTable)Session["GioHang"]),(Int32.Parse(maSP.Text.ToString())),(Int32.Parse(soluong.Text.ToString())));
            }
            LoadData();
        }

        protected void rpDsSpGioHang_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }

        protected void btnCheckOut_Click(object sender, EventArgs e)
        {
            Response.Redirect("CheckOut.aspx");
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dsGioHang.Rows.Count; i++)
            {
                
            }
        }
    }
}